/*
 * @Author: Oscar Pena
 * CS431 2011
 * Represents a Finch Robot with a Terrain.
 * */
package FlowGram.Application.SimulatorWindow.TerrainEditor;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;

public class Finch extends MapObject {

	private Direction Orientation;
	private Color c;
	
	public Finch(int x, int y , Direction d){
		super.setPosition(x, y);	
		Orientation = d;
		c = new Color(0,0,0);
		
	}
	
	public void setOrientation(Direction d){
		//@Description: sets the direction where the finch nose is pointing to
		//@Param: Direction d
		this.Orientation = d;
	}
	public Direction getOrientation(){
		//@Description: Returns the direction where the finch nose is pointing to
		return this.Orientation;
	}
	public boolean equals(Finch b){
		//Description: returns if a Finch is equal to another beeper Object
		return super.equals(b) && b.getOrientation() == this.Orientation;
	}
	public String toString(){
		//@Desccription: returns the beeper as an string
		String ts = "";
		ts = "Finch " + super.toString() + " Orientation = " + this.Orientation + " Color = " + this.c;
		return ts;
	}
	public void Draw(Graphics2D g2d, int scale){
		g2d.setColor(this.getColor());
		double x1=0, x2=0, x3=0, x4=0, x5=0, x6=0, x7 = 0;
		double y1=0, y2=0, y3=0, y4=0, y5=0, y6=0, y7 = 0;
		double thisx = (this.getPositionX() * scale);
		double thisy = (this.getPositionY() * scale);
		x1 = 0;
		y1 = (scale/2) - (scale/16);
		x2 = (scale/8)*3;
		y2 = (scale/8);
		x3 = (scale/8);
		y3 = ((scale/8)*2)* -1;
		x4 = (scale/8)*2;
		y4 = ((scale/2) - (scale/16))* -1;
		x5 = ((scale/8)*2)*-1;
		y5 = y4;
		x6 = x3 * -1;
		y6 = y3;
		x7 = x2 * -1;
		y7 = y2;
		double i;
	
		if (this.Orientation == Direction.EAST){
			i = x1;
			x1 = y1;
			y1 = i;
			i = x2;
			x2 = y2;
			y2 = i;
			i = x3;
			x3 = y3;
			y3 = i;
			i = x4;
			x4 = y4;
			y4 = i;
			i = x5;
			x5 = y5;
			y5 = i;
			i = x6;
			x6 = y6;
			y6 = i;
			i = x7;
			x7 = y7;
			y7 = i;
		} else if (this.Orientation == Direction.SOUTH){
			

			
			
		} else if (this.Orientation == Direction.WEST){
			i = x1;
			x1 = y1*-1;
			y1 = i;
			i = x2;
			x2 = y2*-1;
			y2 = i;
			i = x3;
			x3 = y3*-1;
			y3 = i;
			i = x4;
			x4 = y4*-1;
			y4 = i;
			i = x5;
			x5 = y5*-1;
			y5 = i;
			i = x6;
			x6 = y6*-1;
			y6 = i;
			i = x7;
			x7 = y7*-1;
			y7 = i;
			
		} else if (this.Orientation == Direction.NORTH){
			y1 = y1 * -1;
			y2 = y2 *-1;
			y3 = y3 * -1;
			y4 = y4 * -1;
			y5 = y5 * -1;
			y6 = y6 * -1;
			y7 = y7 * -1;
		}

		x1 = x1 + thisx + (scale/2);
		y1 = y1 + thisy + (scale/2);
		
		x2 = x2 + thisx + (scale/2);
		y2 = y2 + thisy + (scale/2);
		
		x3 = x3 + thisx + (scale/2);
		y3 = y3 + thisy + (scale/2);
		
		x4 = x4 + thisx + (scale/2);
		y4 = y4 + thisy + (scale/2);
		
		x5 = x5 + thisx + (scale/2);
		y5 = y5 + thisy + (scale/2);
		
		x6 = x6 + thisx + (scale/2);
		y6 = y6 + thisy + (scale/2);
		
		x7 = x7 + thisx + (scale/2);
		y7 = y7 + thisy + (scale/2);
		
		
		g2d.setStroke(new BasicStroke(3));
	  	g2d.drawLine((int)x1, (int)y1, (int)x2, (int)y2);
	  	g2d.drawLine((int)x2, (int)y2, (int)x3, (int)y3);
	  	g2d.drawLine((int)x3, (int)y3, (int)x4, (int)y4);
	  	g2d.drawLine((int)x4, (int)y4, (int)x5, (int)y5);
	  	g2d.drawLine((int)x5, (int)y5, (int)x6, (int)y6);
	  	g2d.drawLine((int)x6, (int)y6, (int)x7, (int)y7);
	  	g2d.drawLine((int)x7, (int)y7, (int)x1, (int)y1);
	}

	private Color getColor() {
		// TODO Auto-generated method stub
		return this.c;
	}

	public void moveForward() {
		//@Description move one tile forward in the orientation that is pointing to
		if (this.Orientation == Direction.NORTH){
			this.setPosition(getPositionX(), getPositionY()-1);
		} else if (this.Orientation == Direction.SOUTH){
			this.setPosition(getPositionX(), getPositionY()+1);
		} else if (this.Orientation == Direction.EAST){
			this.setPosition(getPositionX()+1, getPositionY());
		} else if (this.Orientation == Direction.WEST){
			this.setPosition(getPositionX()-1, getPositionY());
		} 
	}

	public void erase(Graphics2D g2d, int scale) {
		//@Description erases current tile before any repainting.
		g2d.setColor(Color.WHITE);
		int thisx = (this.getPositionX() * scale);
		int thisy = (this.getPositionY() * scale);
		g2d.fillRect(thisx+3, thisy+3, scale-3, scale-3);
	}

	public void setColor(Color c2) {
		this.c = new Color(c2.getRed(), c2.getGreen(), c2.getBlue());
	}

	public void turnleft() {
		//@description turns left by checking it's own direction and moving 90 degrees to the left
		if (this.getOrientation() == Direction.NORTH){
			this.setOrientation(Direction.WEST);
		} else if (this.getOrientation() == Direction.EAST){
			this.setOrientation(Direction.NORTH);
		} else if (this.getOrientation() == Direction.SOUTH){
			this.setOrientation(Direction.EAST);
		} else if (this.getOrientation() == Direction.WEST){
			this.setOrientation(Direction.SOUTH);
		}
		
	}

}
